#include "settings.h"
#include "settingsthread.h"
#include "cfgfile.h"

Settings::Settings(QObject *parent) :
    QObject(parent)
{
    qDebug()<<"Settings::Settings()";
    valid = false;
    CurrentStatus *currentStatus = CurrentStatus::getInstance();
    currentStatus->setCurrentStatus(CurrentStatus::CURRENT_STATUS_CONFIGURING);
    if(this->refresh())
    {
        qDebug()<<"this->refresh() OK";
        valid = true;
        currentStatus->setCurrentStatus(CurrentStatus::CURRENT_STATUS_IDLE);
    }
    else
    {
        qDebug()<<"this->refresh() FAIL";
        Sleep(uint(10000));
    }

    _settingsThread = new SettingsThread();
    _timer = new QTimer;

    connect(_timer, SIGNAL(timeout()), _settingsThread, SLOT(runThread()));

    _timer->setInterval((60*1000)*30);
    _timer->start();

    _working = false;
}
Settings::~Settings()
{
    qDebug()<<"Settings::~Settings()";
    if(_timer!=NULL)
        delete _timer;
    if(_settingsThread !=NULL)
    {
        delete _settingsThread;
    }
}

Settings* Settings::_instance = NULL;

Settings* Settings::getInstance()
{
    qDebug()<<"Settings* Settings::getInstance()";
    if(_instance==NULL)
    {
        qDebug()<<"Creating new instance";
        _instance = new Settings;
        if(!_instance->valid)
        {
            delete _instance;
            _instance = NULL;
        }
    }
    qDebug()<<"returning existing instance";

    return _instance;
}
bool Settings::refresh()
{
    qDebug()<<"Settings::refresh()";

    bool result = this->refreshFromWS();
    result &=this->refreshFromCFGFile();
    return result;
}
bool Settings::refreshFromCFGFile()
{
    qDebug()<<"Settings::refreshFromCFGFile()";
    CFGFile cfg;
    this->setServerIP(cfg.getServerIP());
    this->setServerPort(cfg.getServerPort());
    this->setServerPath(cfg.getServerPath());
    return true;
}

bool Settings::refreshFromWS()
{
    qDebug()<<"Settings::refreshFromWS()";
    PelicanoWS ws;
    SettingsResponse response = ws.getSettings();
    if(response.driveLetter=="")
        return false;
    if(getDriveLetter()!=response.driveLetter)
    {
        qDebug()<<"changed this->getDriveLetter: "<<this->getDriveLetter();
        this->setDriveLetter(response.driveLetter);
        emit(changedDriveLetter(response.driveLetter));
    }
    if(response.final_folder_ripping!=""&&getFinalPath()!=response.final_folder_ripping)
    {
        qDebug()<<"changed this->getFinalPath: "<<this->getFinalPath();
        this->setFinalPath(response.final_folder_ripping);
        emit(changedFinalPath(response.final_folder_ripping));
    }
    if(response.temp_folder_ripping!=""&&getTemporalPath()!=response.temp_folder_ripping)
    {
        qDebug()<<"changed this->getTemporalPath"<<this->getTemporalPath();
        this->setTemporalPath(response.temp_folder_ripping);
        emit(changedTempPath(response.temp_folder_ripping));
    }
    if(response.mymovies_password!=""&&getMymoviesPassword()!=response.mymovies_password)
    {
        qDebug()<<"changed this->getMymoviesPassword"<<this->getMymoviesPassword();
        this->setMymoviesPassword(response.mymovies_password);
        emit(changedMyMoviePassword(response.mymovies_password));
    }
    if(response.mymovies_username!=""&&getMymoviesUsername()!=response.mymovies_username)
    {
        qDebug()<<"changed this->getMymoviesUsername"<<this->getMymoviesUsername();
        this->setMymoviesUsername(response.mymovies_username);
        emit(changedMyMovieUsername(response.mymovies_username));
    }
    if(!response.time_from_reboot.isNull()&&getTimeFromReboot()!=response.time_from_reboot)
    {
        qDebug()<<"changed this->getTimeFromReboot"<<this->getTimeFromReboot();
        this->setTimeFromReboot(response.time_from_reboot);
        emit(changedTimeFromReboot(response.time_from_reboot));
    }
    if(!response.time_to_reboot.isNull()&&getTimeToReboot()!=response.time_to_reboot)
    {
        qDebug()<<"changed this->getTimeToReboot"<<this->getTimeToReboot();
        this->setTimeToReboot(response.time_to_reboot);
        emit(changedTimeToReboot(response.time_to_reboot));
    }
    if(response.id_device!=""&&getIdDevice()!=response.id_device)
    {
        qDebug()<<"changed this->getIdDevice"<<this->getIdDevice();
        this->setIdDevice(response.id_device);
        CFGFile cfg;
        cfg.writeValue("id_device",response.id_device);
        emit(changedIdDevice(response.id_device));
    }
    return true;
}

void Settings::setDriveLetter(QString value)
{
    _driveLetter = value;
}

void Settings::setTemporalPath(QString value)
{
    _temporalPath = value;
}

void Settings::setFinalPath(QString value)
{
    _finalPath = value;
}
void Settings::setServerIP(QString value)
{
    _serverIP = value;
}
void Settings::setServerPath(QString value)
{
    _serverPath = value;
}
void Settings::setServerPort(int value)
{
    _serverPort = value;
}
void Settings::setMymoviesUsername(QString value)
{
    _mymoviesUsername = value;
}

void Settings::setMymoviesPassword(QString value)
{
    _mymoviesPassword = value;
}

void Settings::setTimeFromReboot(QTime value)
{
    _timeFromReboot = value;
}

void Settings::setTimeToReboot(QTime value)
{
    _timeToReboot = value;

}
void Settings::setIdDevice(QString value)
{
    _idDevice = value;
}


QString Settings::getDriveLetter()
{
    return _driveLetter;
}

QString Settings::getTemporalPath()
{
    return _temporalPath;
}

QString Settings::getFinalPath()
{
    return _finalPath;
}

QString Settings::getServerIP()
{
    return _serverIP;
}

int Settings::getServerPort()
{
    return _serverPort;
}

QString Settings::getServerPath()
{
    return _serverPath;
}
QString Settings::getMymoviesUsername()
{
    return _mymoviesUsername;
}

QString Settings::getMymoviesPassword()
{
    return _mymoviesPassword;
}

QTime Settings::getTimeFromReboot()
{
    return _timeFromReboot;
}

QTime Settings::getTimeToReboot()
{
    return _timeToReboot;
}
QString Settings::getIdDevice()
{
    return _idDevice;
}
